//
//  SISSPassengerCoopTakeLookRecordViewController.m
//  SISSAPP
//
//  Created by 优房网 on 2017/8/9.
//  Copyright © 2017年 优房网. All rights reserved.
//

#import "SISSPassengerCoopTakeLookRecordViewController.h"
#import "SISSPassengerTakeLookRecordCell.h"
#import "SISSPassengerCoopTakeLookDetailViewController.h"
#import "SISSAddPassengerCoopTakeLookViewController.h"
#import "SISSNetworkingManager+CoopProgress.h"

#import "SISSPassengerCoopProgressModel.h"

@interface SISSPassengerCoopTakeLookRecordViewController ()

/* 带看模型数组 */
@property (strong, nonatomic) NSArray<SISSPassengerCoopTakeLookModel *> *takeLookArr;

@end

@implementation SISSPassengerCoopTakeLookRecordViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    WeakSelf
    [kSISSRefresh prepareHeaderRefresh:self.tableView loadDataBlock:^{
        kSISSRefresh.refreshType = SISSRefressTypeUpDown;
        [weakSelf upDownLoadData];
    }];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    [self requestPassengerCooperationTakeLookRecordData];
}

- (void)addRecordButtonDidClickAction {
    [super addRecordButtonDidClickAction];
    
    SISSAddPassengerCoopTakeLookViewController *vc = [[SISSAddPassengerCoopTakeLookViewController alloc] init];
    vc.passengerCoopProgressModel = self.passengerCoopProgressModel;
    [self.navigationController pushViewController:vc animated:YES];
}

#pragma mark - UITableViewDelegate & UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.takeLookArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    SISSPassengerTakeLookRecordCell *cell = [tableView dequeueReusableCellWithIdentifier:passengerTakeLookRecordCellReuseID forIndexPath:indexPath];
    cell.takeLookModel = self.takeLookArr[indexPath.row];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    
    SISSPassengerCoopTakeLookDetailViewController *vc = [[SISSPassengerCoopTakeLookDetailViewController alloc] init];
    vc.passengerCoopProgressModel = self.passengerCoopProgressModel;
    vc.deleteCompletedBlock = ^{
        
        [self requestPassengerCooperationTakeLookRecordData];
    };
    vc.brokerState = self.takeLookArr[indexPath.row].brokerState;
    vc.takeLookModel = self.takeLookArr[indexPath.row];
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)upDownLoadData {
    
    [self requestPassengerCooperationTakeLookRecordData];
}

#pragma mark - 获取客源合作带看记录数据
- (void)requestPassengerCooperationTakeLookRecordData {
    
    [kNetworkManager requestPassengerCooperationTakeLookWithAcbId:self.passengerCoopProgressModel.ID success:^(NSArray<SISSPassengerCoopTakeLookModel *> *takeLookArr) {
        self.takeLookArr = takeLookArr;
        
        [[NSOperationQueue mainQueue] addOperationWithBlock:^{
            [self.tableView reloadData];
            [self.tableView.mj_header endRefreshing];
        }];

        
    } failed:^(NSError *error) {
        [self.tableView.mj_header endRefreshing];
        NSLog(@"%@",error);
    }];
}


@end
